package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketFeedback;
import com.cskaoyan.market.db.domain.MarketFeedbackExample;
import com.cskaoyan.market.db.mapper.MarketFeedbackMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminFeedbackServiceImpl implements AdminFeedbackService {
    @Override
    public List<MarketFeedback> list(final String page, final String limit, final String sort, final String order, final String username, final String id) {
        SqlSession session = MybatisUtils.getSession();
        MarketFeedbackMapper marketFeedbackMapper = session.getMapper(MarketFeedbackMapper.class);
        MarketFeedbackExample marketFeedbackExample = new MarketFeedbackExample();
        MarketFeedbackExample.Criteria criteria = marketFeedbackExample.createCriteria();

        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.parseInt(id));
        }

        marketFeedbackExample.setOrderByClause(sort + " " + order);

        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));

        List<MarketFeedback> marketFeedbackList = marketFeedbackMapper.selectByExample(marketFeedbackExample);
        session.commit();
        session.close();
        return marketFeedbackList;
    }
}
